package com.alibaba.otter.manager.web.resource;


import com.alibaba.otter.manager.biz.statistics.throughput.ThroughputStatService;
import com.alibaba.otter.manager.biz.statistics.throughput.param.*;
import com.alibaba.otter.manager.web.dto.HistoryThroughputInfoDTO;
import com.alibaba.otter.manager.web.dto.RecentThroughputStatDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.service.ThroughputStatAggService;
import com.alibaba.otter.shared.common.model.statistics.throughput.ThroughputStat;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/v1/throughput")
@RestController
public class ThroughputStatResource {


    private final ThroughputStatService throughputStatService;

    private final ThroughputStatAggService throughputStatAggService;

    public ThroughputStatResource(ThroughputStatService throughputStatService, ThroughputStatAggService throughputStatAggService) {
        this.throughputStatService = throughputStatService;
        this.throughputStatAggService = throughputStatAggService;
    }


    @GetMapping("recent-throughput-stat")
    public RestResponse<List<RecentThroughputStatDTO>> findRecentThroughputStatByPipelineId(@RequestParam("pipelineId") Long pipelineId) {
        return RestResponse.ok(throughputStatAggService.findRecentThroughputStatByPipelineId(pipelineId));
    }

    @GetMapping("history-throughput-stat")
    public RestResponse<HistoryThroughputInfoDTO> findHistoryThroughputStatByPipelineId(@RequestParam(required = false,value = "startTime") Instant startTime,
                                                                                        @RequestParam(required = false,value = "endTime") Instant endTime,
                                                                                        @RequestParam("pipelineId") Long pipelineId) {
        return RestResponse.ok(throughputStatAggService.historyThroughput(startTime,endTime,pipelineId));
    }

    /**
     * 实时吞吐量列表
     */
    @GetMapping("list-realtime-throughput")
    public RestResponse<Map<AnalysisType, ThroughputInfo>> listRealtimeThroughput(RealtimeThroughputCondition condition){
        return RestResponse.ok(throughputStatService.listRealtimeThroughput(condition));
    }


    /**
     * <pre>
     * 列出pipeLineId下，start-end时间段下的throughputStat
     * 首先从数据库中取出这一段时间所以数据，该数据都是根据end_time倒排序的, 每隔1分钟将这些数据分组
     * </pre>
     */
    @GetMapping("list-timeline-throughput")
    public RestResponse<Map<Long, ThroughputInfo>> listTimelineThroughput(TimelineThroughputCondition condition){
        return RestResponse.ok(throughputStatService.listTimelineThroughput(condition));
    }


    /**
     * 根据pipeline-id查询实时吞吐量列表
     */
    @GetMapping("list-realtime-throughput-by-pipeline-ids/{minus}")
    public RestResponse<List<ThroughputStat>> listRealtimeThroughputByPipelineIds(@RequestParam("pipelineIds") List<Long> pipelineIds, @PathVariable("minus") int minute){
        return RestResponse.ok(throughputStatService.listRealtimeThroughputByPipelineIds(pipelineIds,minute));
    }


    /**
     * 实时吞吐量列表
     */
    @GetMapping("find-throughput-stat-by-pipeline-id")
    public RestResponse<ThroughputStat> findThroughputStatByPipelineId(ThroughputCondition condition){
        return RestResponse.ok(throughputStatService.findThroughputStatByPipelineId(condition));
    }


    /**
     * 实时吞吐量列表
     */
    @PostMapping("create-or-update-throughput")
    public RestResponse<Void> createOrUpdateThroughput(@RequestBody ThroughputStat item){
        throughputStatService.createOrUpdateThroughput(item);
        return RestResponse.ok();
    }

}
